<?php
error_reporting(0);
header('Content-Type: text/html; charset=utf-8');
include_once('../comm/config.php');
include '../comm/msgset.php';
include 'check.php';
$path = dirname(dirname(__FILE__)) . '/data/phpwindapi.php';
$action = trim($_GET['action']);

function add_user($username, $userpwd, $email)
{
    //忽略非正常数据
    global $BIAOTOU;
    if (strlen($username) < 3 || strlen($userpwd) < 5 || strlen($email) < 4) {
        return 0;
    }
    $qz = "SELECT count(*) FROM " . $BIAOTOU . "user where ddusername='" . $username . "' or email='" . $email . "'";
    $rs = mysql_query($qz);
    $t = mysql_fetch_array($rs);
    if ($t[0] > 0) {
        echo "用户： $username - $email 不能整合，因为duoduo系统里已有该用户名或邮箱名\r\n<br/>";
        return 0;
    }
    $md5pwd = $userpwd;
    $sj = date('Y-m-d H:i:s');
    $qq = 11111;

    $insert = "INSERT INTO " . $BIAOTOU . "user(Id,ddusername,ddpassword,email,qq,regtime,lastlogintime,loginnum,tjr,pass_question,pass_answer,money,jifen,dengji,jihuo) values (NULL,'$username','$md5pwd','$email','$qq','$sj','$sj',1,'','','','" . ZHUCESONG . "','" . ZSJIFEN . "'," . ZHUCESONGLEVEL . ",1)";
    mysql_query($insert);
    
    $tg = round(TGBL / FXBL * 100, 2);
    global $msg_zhuce;
    $msg_zhuce = str_replace("[name]", $username, $msg_zhuce);
    $msg_zhuce = str_replace("[WEBTITLE]", WEBTITLE, $msg_zhuce);
    $msg_zhuce = str_replace("[tg]", $tg, $msg_zhuce);
    $insert = "INSERT into " . $BIAOTOU . "msg(ddusername,title,content,addtime,senduser) values ('$username','欢迎注册" . WEBTITLE . "','" . $msg_zhuce . "','" . $sj . "','网站客服')";
    mysql_query($insert);
    if (ZHUCESONG != 0) {
        $insert = "INSERT INTO " . $BIAOTOU . "mingxi(ddusername,shijian,je,addtime,memo) values ('" . $username . "','注册赠送','" . ZHUCESONG . "','" . $sj . "','注册赠送金额" . ZHUCESONG . "元')";
        mysql_query($insert);
        global $msg_zhucesong;
        $msg_zhucesong = str_replace("[ZHUCESONG]", ZHUCESONG, $msg_zhucesong);
        $insert = "INSERT into " . $BIAOTOU . "msg(ddusername,title,content,addtime,senduser) values ('$username','注册赠送','" . $msg_zhucesong . "','$sj','网站客服')";
        mysql_query($insert);
    }

    if (JIFENOPEN == 1 && ZSJIFEN > 0) {
        $insert = "INSERT INTO " . $BIAOTOU . "mingxi(ddusername,shijian,jifen,addtime,memo) values ('" . $username . "','注册赠送积分','" . ZSJIFEN . "','" . $sj . "','注册赠送积分" . ZSJIFEN . "')";
        mysql_query($insert);
        global $msg_zsjifen;
        $msg_zsjifen = str_replace("[ZSJIFEN]", ZSJIFEN, $msg_zsjifen);
        $insert = "INSERT into " . $BIAOTOU . "msg(ddusername,title,content,addtime,senduser) values ('$username','注册赠送积分','" . $msg_zsjifen . "','$sj','网站客服')";
        mysql_query($insert);
    }
    return 1;
}

if ($action == 'run') {
    header('Content-Type: text/html; charset=utf-8');
    $windconfig = get_phpwind_config();
    if ($windconfig['open'] != 'on') {
        echo '<script language=javascript>alert("您的整合参数配置有误！");window.location="getphpwinduser.php";</script>';
        exit;
    }
    $pwd = $windconfig['pwd'];
    $checkurl = $windconfig['phpdomain'] . '/hack/duoduo/comm.php?action=getuser&pwd=' . $pwd;
    $res = trim(file_get_contents($checkurl));
    $row = split("\n", $res);
    $num = 0;
    foreach ($row as $u)
    {
        $userarr = split("\t", $u);
        $sta = add_user($userarr[0], $userarr[1], $userarr[2]);
        if ($sta == 1) {
            $num++;
        }
    }
    echo "<br/><br/>已经成功整合 $num 个用户，请关闭本页面";
    exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>管理中心</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>

<body topmargin="5" leftmargin="0">
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="282" valign="top"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td>
          <table cellspacing="0" width="100%" height="207" style="border:1px  solid #DCEAF7;">
            <tr>
              <td height="30" colspan="2" bgcolor="E9F2FB" class="bigtext"><strong>&nbsp;<img src="images/arrow.gif" width="16" height="22" align="absmiddle" />phpwind会员导入操作</strong></td>
            </tr>
            <tr>
              <td width="100%" colspan="2" valign="top">
              <table width="100%" height="300" border="0" cellpadding="0" cellspacing="0">
                注意：导入Phpwind会员之前，最好确保本程序没有注册会员，如果本程序已经有会员，将可能出现phpwind与本程序的会员冲突（即用户名或邮箱名重复）。<br/>
        对于这种情况的处理方式是：存在重复用户名或邮箱名的会员将不执行导入操作。
        <br/>
        要导入phpwind用户，请首先确保与phpwind整合的各项参数设置正确，然后点击下面按钮即可
        <br/><br/>
        <form method="get" action="">
            <input type="hidden" name="action" value="run"/>
            <input type="submit" value=" 开始导入phpwind用户 " />
        </form>
               
              </table>
              </td>
            </tr>
          </table></td>
        </tr>
      
    </table></td>
  </tr>
</table>
</body>
</html>